enum YINXIAO {
    //% block="普通"
    0,
    //% block="流行"
    1,
    //% block="摇滚"
    2,
    //% block="爵士"
    3,
    //% block="古典"
    4,
    //% block="低音"
    5
}

enum GONGNENG {
    //% block="音量加"
    0,
    //% block="音量减"
    1,
    //% block="上一曲"
    2,
    //% block="下一曲"
    3,
    //% block="播放"
    4,
    //% block="暂停"
    5,
    //% block="停止播放曲目"
    6,
    //% block="循环播放开始"
    7,
    //% block="循环播放停止"
    8,
    //% block="循环播放所有曲目"
    9,
    //% block="停止循环播放曲目"
    10,
    //% block="随机播放所有曲目"
    11,
    //% block="开启音频输出"
    12,
    //% block="关闭音频输出"
    13,
    //% block="进入睡眠状态"
    14,
    //% block="复位"
    15
}

enum DETD {
    //% block="当前状态"
    0,
    //% block="当前音量"
    1,
    //% block="当前音效"
    2,
    //% block="总文件数"
    3,
    //% block="当前播放曲目号"
    4
}

//% color="#660099" iconWidth=50 iconHeight=40
namespace miniMP3 {
    //% block="初始化miniMP3模块通讯串口，RX[YINJIAO1]，TX[YINJIAO2]" blockType="command"
    //% YINJIAO1.shadow="dropdown" YINJIAO1.options="PIN_DigitalWrite"
    //% YINJIAO2.shadow="dropdown" YINJIAO2.options="PIN_DigitalWrite"
    export function miniMP3_init(parameter: any, block: any) {
        let yinjiao1 = parameter.YINJIAO1.code;
        let yinjiao2 = parameter.YINJIAO2.code;

        Generator.addInclude("includesoftSerial", "#include <SoftwareSerial.h>");
        Generator.addInclude("miniMP3", "#include <Arduino.h>");
        Generator.addInclude("includeminiMP3", "#include <DFRobotDFPlayerMini.h>");
        Generator.addObject(`softSerialObject`, `SoftwareSerial`, `mySerial(${yinjiao1}, ${yinjiao2});`);
        Generator.addObject(`miniMP3Object`, `DFRobotDFPlayerMini`, `myPlayer;`);
        Generator.addSetup(`softSerialSetup`, `mySerial.begin(9600);`);
        Generator.addSetup(`miniMP3Setup`, `myPlayer.begin(mySerial);`);
     }

    //% block="设置miniMP3模块音量为[YINLIANG]" blockType="command"
    //% YINLIANG.shadow="range" YINLIANG.params.min=0 YINLIANG.params.max=220 YINLIANG.defl=15
    export function miniMP3_volume(parameter: any, block: any) {
        let yinliang = parameter.YINLIANG.code;

        Generator.addCode(`myPlayer.volume(${yinliang});`);
     }

    //% block="设置miniMP3模块音效为[YINXIAO]" blockType="command"
    //% YINXIAO.shadow="dropdown" YINXIAO.options="YINXIAO" YINXIAO.defl="0"
    export function miniMP3_EQ(parameter: any, block: any) {
        let yinxiao = parameter.YINXIAO.code;

        Generator.addCode(`myPlayer.EQ(${yinxiao});`);
     }

    //% block="miniMP3模块播放曲目[NUM]" blockType="command"
    //% NUM.shadow="range" NUM.params.min=1 NUM.params.max=999 NUM.defl=1
    export function miniMP3_play1(parameter: any, block: any) {
        let num = parameter.NUM.code;

        Generator.addCode(`myPlayer.play(${num});`);
     }

    //% block="miniMP3模块播放文件夹[NUM1]内的曲目[NUM2]" blockType="command"
    //% NUM1.shadow="range" NUM1.params.min=1 NUM1.params.max=999 NUM1.defl=1
    //% NUM2.shadow="range" NUM2.params.min=1 NUM2.params.max=999 NUM2.defl=1
    export function miniMP3_play2(parameter: any, block: any) {
        let num1 = parameter.NUM1.code;
        let num2 = parameter.NUM2.code;

        Generator.addCode(`myPlayer.playFolder(${num1}, ${num2});`);
     }

    //% block="miniMP3模块循环播放文件夹[NUM3]内的曲目" blockType="command"
    //% NUM3.shadow="range" NUM3.params.min=1 NUM3.params.max=999 NUM3.defl=1
    export function miniMP3_play3(parameter: any, block: any) {
        let num3 = parameter.NUM3.code;

        Generator.addCode(`myPlayer.loopFolder(${num3});`);
     }

    //% block="miniMP3模块[GONGNENG]" blockType="command"
    //% GONGNENG.shadow="dropdown" GONGNENG.options="GONGNENG" GONGNENG.defl="0"
    export function miniMP3_MOD(parameter: any, block: any) {
        let gongneng = parameter.GONGNENG.code;

        if(gongneng == 0)
        {
            Generator.addCode(`myPlayer.volumeUp( );`);
        }
        else if(gongneng == 1)
        {
            Generator.addCode(`myPlayer.volumeDown( );`);
        }
        else if(gongneng == 2)
        {
            Generator.addCode(`myPlayer.previous( );`);            
        }
        else if(gongneng == 3)
        {
            Generator.addCode(`myPlayer.next( );`);
        }
        else if(gongneng == 4)
        {
            Generator.addCode(`myPlayer.start( );`);
        }
        else if(gongneng == 5)
        {
            Generator.addCode(`myPlayer.pause( );`);
        }
        else if(gongneng == 6)
        {
            Generator.addCode(`mmyPlayer.stopAdvertise( );`);
        }
        else if(gongneng == 7)
        {
            Generator.addCode(`myPlayer.enableLoop( );`);
        }
        else if(gongneng == 8)
        {
            Generator.addCode(`myPlayer.disableLoop( );`);
        }
        else if(gongneng == 9)
        {
            Generator.addCode(`myPlayer.enableLoopAll( );`);
        }
        else if(gongneng == 10)
        {
            Generator.addCode(`myPlayer.disableLoopAll( );`);
        }
        else if(gongneng == 11)
        {
            Generator.addCode(`myPlayer.randomAll( );`);
        }
        else if(gongneng == 12)
        {
            Generator.addCode(`myPlayer.enableDAC( );`);
        }
        else if(gongneng == 13)
        {
            Generator.addCode(`myPlayer.disableDAC( );`);
        }
        else if(gongneng == 14)
        {
            Generator.addCode(`myPlayer.sleep( );`);
        }
        else if(gongneng == 15)
        {
            Generator.addCode(`myPlayer.reset( );`);
        }
     }

    //% block="miniMP3模块获取[DETD]" blockType="reporter"
    //% DETD.shadow="dropdown" DETD.options="DETD" DETD.defl="0"
    export function miniMP3_READ1(parameter: any, block: any) {
        let detd = parameter.DETD.code;

        if(detd == 0)
        {
            Generator.addCode([`myPlayer.readState( )`, Generator.ORDER_UNARY_POSTFIX]]);
        }
        else if(detd == 1)
        {
            Generator.addCode([`myPlayer.readVolume( )`, Generator.ORDER_UNARY_POSTFIX]]);
        }
        else if(detd == 2)
        {
            Generator.addCode([`myPlayer.readEQ( )`, Generator.ORDER_UNARY_POSTFIX]]);           
        }
        else if(detd == 3)
        {
            Generator.addCode([`myPlayer.readFileCounts(DFPLAYER_DEVICE_SD)`, Generator.ORDER_UNARY_POSTFIX]]);
        }
        else if(detd == 4)
        {
            Generator.addCode([`myPlayer.readCurrentFileNumber(DFPLAYER_DEVICE_SD)`, Generator.ORDER_UNARY_POSTFIX]]);
        }
    }

    //% block="miniMP3模块获取文件夹[NUM4]的文件数" blockType="reporter"
    //% NUM4.shadow="range" NUM4.params.min=1 NUM4.params.max=999 NUM4.defl=1
    export function miniMP3_READ2(parameter: any, block: any) {
        let num4 = parameter.NUM4.code;

        Generator.addCode([`myPlayer.readFileCountsInFolder(${num4})`, Generator.ORDER_UNARY_POSTFIX]]);
    }
}